/**
 * 
 */
package com.neotiq.smartdevice;

import com.neotiq.smartdevice.model.DeviceManager;
import com.neotiq.smartdevice.model.DeviceType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author hbriand
 *
 */
public class DeviceAdder extends Activity {
    public static final String TAG = "SmartDeviceAdder";

    static final String[] DEVICES = new String[] {
        "Button", "Checkbox", "ToggleButton", "RadioButton", "Spinner", "ProgressBar", "SeekBar",
    };
    
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device_adder);
        
        ListView v = (ListView)findViewById(R.id.listView1);
        v.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, DEVICES));
        
        v.setTextFilterEnabled(true);
        v.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (((TextView) view).getText().equals("Button") || ((TextView) view).getText().equals("ToggleButton")) {
                    addDevice((String)((TextView) view).getText());
                } else {
                    Toast.makeText(getApplicationContext(), "Not supported yet", Toast.LENGTH_SHORT).show();
                }
            }
            
        });
    }

    protected void addDevice(String text) {
        DeviceType dt;
        if (text.equals("Button")) {
            dt = DeviceType.BUTTON;
        } else if (text.equals("ToggleButton")) {
            dt = DeviceType.TGL_BTN;
        } else {
            return;
        }
        DeviceManager.addDevice(dt);
        Intent i = new Intent(this, DevicesView.class);
        startActivity(i);            
    }

}
